#pragma once

#include <thread>
#include <string>
#include <memory>
#include <chrono>
#include "model/AbstractNode.h"
#include "base/Bytes.h"
#include "base/Define.h"

namespace zeroplus::mq
{
    class SubscriberNode : public AbstractNode
    {
    public:
        bool isListened_;

    private:
        std::string topic_;
        std::thread th_;
        OnMsgRecvCb processMsgCb_;

    public:
        SubscriberNode(const std::string &uri, const ConnectType &connectType = connect)
            : isListened_(false)
        {
            CreateConnection(zmqpp::socket_type::sub, uri, connectType);
        }

        ~SubscriberNode()
        {
            if (th_.joinable())
            {
                th_.join();
            }
        }

        void Subscribe(const std::string &topic)
        {
            topic_ = topic;
            conn_->SetSubscribe(topic_);
            LOG_INFO("订阅%s", topic_.c_str());
        }

        bool Run(const OnMsgRecvCb &processMsgCb);

        Bytes Recv();
    };
}
